<?php

/**
 * Created by PhpStorm.
 * User: allen_ios
 * Date: 17/6/13
 * Time: 上午10:24
 */
class BaseCartService extends BaseService
{

    const REDIS_CACHE_EXPIRE = 600;
    private $cacheKey;
    private $restaurantId;     //餐厅id
    public function __construct()
    {
        $params = func_get_args();
        $this->restaurantId = $params[0][0];
        $this->iPadId = $params[0][1];
        //$this->cacheKey =  'zdu_restaurant_cart_'.$this->restaurantId.'_*';
        $this->cacheKey =  'zdu_restaurant_cart_'.$this->restaurantId.$this->iPadId.'-*';

    }
    /**
     * 获得购物车列表信息
     * @return
     */
    public function getRestaurantCartList()
    {
        // 存贮哈希结构
        $cartKeys = RedisService::keys($this->cacheKey,'hash');
        if (!$cartKeys) {
            return null;
        }
        $cartList  = array();
        foreach ($cartKeys as $key) {
            $key = substr($key,5);
            $cartList[] = RedisService::hget($key);
        }
        return $cartList;
    }

    /**
     * 清空购物车
     * @return bool
     */
    public function emptyRestaurantCardList(){
        
        $cartkeys = RedisService::keys($this->cacheKey,'hash');
        if (!$cartkeys) {
            return false;
        }

        foreach ($cartkeys as $key) {
             RedisService::rm($key);
        }
        return true;
    }

    public function getCartGoodsNum($goods_id,$iPadId){

        $key = 'zdu_restaurant_cart_'.$this->restaurantId.'_'.$iPadId.'_'.$goods_id;

        $isHas = RedisService::keyExists($key,'hash');
        if (!$isHas){
            return 0;
        }
        $cartIInfo  = RedisService::hget($key);
        if ($cartIInfo) {
            return $cartIInfo['cart_num'];
        }else{
            return 0;
        }
    }


    /**
     * 操作购物车
     * @param int $goods_id  商品id
     * @param string  $goods_name  商品名称
     * @param int $goodsNum 商品数量
     * @param string $op    操作类型，0:添加购物车，1:删除购物车
     */
    public function doCart($goods_id,$goods_name,$goodsNum,$op=0)
    {

        $key = 'zdu_restaurant_cart_'.$this->restaurantId.$this->iPadId.'_'.$goods_id;

        $flag = RedisService::keyExists( $key,'hash');//默认string类型

        if (!$op){

            if (!$flag){
                // 新增
                $array = array(
                    'goods_id'=>$goods_id,
                    'goods_name'=>$goods_name,
                    'cart_num'=>$goodsNum,
                );
                $res = RedisService::hset($key,$array,self::REDIS_CACHE_EXPIRE);
                return $goodsNum;
            }else{
                $num = RedisService::hIncr($key,'cart_num',$goodsNum);
                return $num;
            }
        }else{
           if ($flag){
               $num = RedisService::hIncr($key,'cart_num',-$goodsNum);
               return $num;
           }
        }
        
    }

    /**
     * 更新购物车
     * @param $goods_id
     * @param $data
     */
    public function updateCart($goods_id,$data){
        
        $key = 'zdu_restaurant_cart_'.$this->restaurantId.$this->iPadId.'_'.$goods_id;

        $res = RedisService::hset($key,$data,self::REDIS_CACHE_EXPIRE);

        return $res;
    }

    /**
     * 删除购物车
     * @param $goods_id
     * @return bool
     */
    public function deleteCart($goods_id){

        $key = 'zdu_restaurant_cart_'.$this->restaurantId.$this->iPadId.'_'.$goods_id;
        // 清除缓存
        $flag = RedisService::keyExists( $key,'hash');//默认string类型

        if ($flag){

            RedisService::rm('hash_'. $key);

        }

        return true;
    }


}